<?php

namespace App;

use App\Http\Instance\Singleton;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use Yansongda\Pay\Pay;
use App\Http\Service\Impl\OrdersImpl;

class Goodslist extends Model
{
    //
    protected $table = "goodslist";
    protected $keyType = 'string';
    private $Ordersservice;

    /**
     * 获取商品列表
     * @return mixed
     */
    public function getList()
    {
        return self::limit(20)->get();
    }

    /**
     * 获取轮播图
     * getCarousel
     */
    public function getCarousel()
    {
        return self::orderBy("createtime")->where("carousel", "1")->limit(10)->get();
    }

    /**
     * 获取商品信息
     * getGoodInfo
     */
    public function getGoodInfo($gid)
    {
        $goodinfo = self::find($gid);//DB::table("goodslist")->where("id", $gid)->first();
        $userid   = Session::get("userid");//查询当前用户是否购买了当前模型,
        //***************************************************************
        date_default_timezone_set("Asia/Shanghai");
        $where['goodid']   = $gid;
        $where['userid']   = $userid;
        $where['paystate'] = 2;//只查已支付的
        $curtime           = date('Y-m-d H:i:s', time());//'2020-05-01 13:21:00';//date('Y-m-d H:i:s',time());//当前时间
        $data              = DB::table('payorders')->where($where)->orderBy("createtime", 'desc')->first();
        if ($data) {
            //如果有数据，继续判断当前日期是否在购买模型的有效期内，如果在的话，就提示不需要重复购买，否则，正常购买
            if ($curtime > $data->stime && $curtime < $data->etime) {
                $goodinfo['already_shop'] = 1;//已购，并且在有效期内
            } else {
                $goodinfo['already_shop'] = 2;//已购，但是不在有效期内,需重新购买
            }
        } else {
            //如果没有数据
            $goodinfo['already_shop'] = 2;//未购
        }


        //***************************************************************
        return $goodinfo;
        ///if (!$goodinfo) return redirect('/error.404');
    }

    /**
     * 阿里支付模型
     * tradePayModel
     * 阿里电脑支付接口
     * 1金点    扣除用户的账户金点
     * 2扫码    跳转支付宝/微信扫码支付页面
     * 以上两种支付方式均：支付成功之后跳转到详情页面，并提示支付成功，并且可以下载
     */
    public function tradePayModel($gid, $type)
    {
        if ($type == 2) {
            $goodinfo = self::getGoodInfo($gid);
            //支付宝支付
            //系统插入定单->跳转支付宝->支付/未支付
            //购买商品
            $this->tradeno = \Tools::settrade();
            $custom        =  array(
                "tradeno"  => $this->tradeno,//产生订单号
                "userid"   => Session::get("userid"),//获取userid
                "goodid"   => $gid,//获取商品id
                "payact"   => 1,
                "price"    => $goodinfo->price,
                "goodname" => $goodinfo->goodname
            );
            $payOrderModel = new Payorder();
            //$Ordersservice = new OrdersImpl();
            //$Ordersservice->insertguestorder($custom);//插入订单
            $payOrderModel->insertguestorder($custom);
            $order  = ['out_trade_no' => $this->tradeno, 'total_amount' => $goodinfo->price, 'subject' => '模型购买'];
            $result = Pay::alipay(Singleton::getInstance()->getConfig())->web($order);
        } else if ($type == 1) {
            //支付宝充值
            $goodinfo      = DB::table('tclist')->where("tccode", $gid)->first();
            $this->tradeno = \Tools::settrade();
            $custom        = array(
                "tradeno" => $this->tradeno,//产生订单号
                "userid"  => Session::get("userid"),//获取userid
                "goodid"  => $gid,//获取商品id
                "payact"  => 2,
                "price"   => $goodinfo->price,
                "goodname" => "金点充值"
            );
            $payOrderModel = new Payorder();
            $payorderinfo = $payOrderModel->insertguestorder($custom);
            $order  = ['out_trade_no' => $this->tradeno, 'total_amount' => $goodinfo->price, 'subject' => '金点充值'];
            addsession("orderid",$payorderinfo->orderid);
            $result = Pay::alipay(Singleton::getInstance()->getConfig())->web($order);
        } else if ($type == 3) {
            //金点支付---扣除账户金点
            //$userid = Session::get("userid");
            $goodinfo = self::find($gid);
            $userinfo = User::find(Session::get("userid"));///DB::table('users')->where("id",$userid)->first();
            //$ups['gold'] = $userinfo->gold-$goodinfo['score'];//扣减之后的价格
            User::where("id", $userid)->update(["gold" => $userinfo->gold - $goodinfo['score']]);
            $result['gold_pay_flag'] = 1;
        }
        return $result;
    }

    public function updateGoodInfo()
    {

    }
}
